package com.farm.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.farm.entity.Order;
import com.farm.entity.Product;
import com.farm.entity.User;
import com.farm.service.OrderService;
import com.farm.service.ProductService;
import com.farm.service.UserService;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/dashboard")
    public String dashboard(Model model) {
        User currentUser = userService.getCurrentUser();
        model.addAttribute("user", currentUser);
        return "admin/dashboard";
    }

    // 用户管理
    @GetMapping("/users")
    public String userList(Model model) {
        // TODO: 实现用户列表查询
        return "admin/user/list";
    }

    @PostMapping("/users/{userId}/audit")
    @ResponseBody
    public String auditUser(@PathVariable Long userId, @RequestParam Integer status) {
        try {
            userService.auditUser(userId, status);
            return "success";
        } catch (Exception e) {
            return "error: " + e.getMessage();
        }
    }

    @PostMapping("/users/{userId}/recharge")
    @ResponseBody
    public String rechargeBalance(@PathVariable Long userId, @RequestParam BigDecimal amount) {
        try {
            userService.recharge(userId, amount);
            return "success";
        } catch (Exception e) {
            return "error: " + e.getMessage();
        }
    }

    // 产品管理
    @GetMapping("/products")
    public String productList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer size,
                            Model model) {
        IPage<Product> products = productService.getProductList(page, size, null);
        model.addAttribute("products", products);
        return "admin/product/list";
    }

    @PostMapping("/products/{productId}/audit")
    @ResponseBody
    public String auditProduct(@PathVariable Long productId, @RequestParam Integer status) {
        try {
            productService.auditProduct(productId, status);
            return "success";
        } catch (Exception e) {
            return "error: " + e.getMessage();
        }
    }

    // 订单管理
    @GetMapping("/orders")
    public String orders(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Model model) {
        // 获取所有订单
        IPage<Order> orderPage = orderService.getAllOrders(page, size);
        model.addAttribute("orders", orderPage);
        return "admin/orders";
    }

    // 统计分析
    @GetMapping("/statistics")
    public String statistics(Model model) {
        // TODO: 实现统计分析
        return "admin/statistics";
    }


    @GetMapping("/settings")
    public String settings(Model model) {
        // 这里可以添加系统设置页面所需的数据
        return "admin/settings";
    }
} 